#include  "hero.h"
#include <iostream>
#include <mysql/mysql.h>
#include <functional>

using namespace std;

hero::hero(/* args */)
{
    // 初始化成员变量
    mHeroName = "";
    mConn = nullptr;
    // 连接到数据库
    if (!connectToDatabase())
    {
        std::cerr << "Failed to connect to database" << std::endl;
    }
}

hero::~hero()
{
    // 断开数据库连接
    if (!disconnectFromDatabase())
    {
        std::cerr << "Failed to disconnect from database" << std::endl;
    }
}

int hero::showHero()
{
    MYSQL_RES *res;
    MYSQL_ROW row;

    // 执行查询
    if (mysql_query(mConn, "SELECT * FROM users"))
    {
        std::cerr << "SELECT failed: " << mysql_error(mConn) << std::endl;
        mysql_close(mConn);
        return EXIT_FAILURE;
    }

    // 获取结果集
    res = mysql_store_result(mConn);
    if (res == nullptr)
    {
        std::cerr << "mysql_store_result() failed: " << mysql_error(mConn) << std::endl;
        mysql_close(mConn);
        return EXIT_FAILURE;
    }

    // 遍历结果集
    int num_fields = mysql_num_fields(res);
    while ((row = mysql_fetch_row(res)))
    {
        for (int i = 0; i < num_fields; ++i)
        {
            std::cout << (row[i] ? row[i] : "NULL") << "\t";
        }
        std::cout << std::endl;
    }

    // 释放结果集和关闭连接
    mysql_free_result(res);
//    mysql_close(mConn);

    return EXIT_SUCCESS;
}

//根据名字删除英雄
bool hero::deleteHero(std::string name)
{
    // 这里可以实现删除英雄的逻辑
    MYSQL_STMT *stmt = mysql_stmt_init(mConn);
    if (!stmt)
    {
        std::cerr << "mysql_stmt_init() failed" << std::endl;
        return false;
    }

    // 准备 SQL 语句
    const char *query = "DELETE FROM users WHERE name = ?";
    if (mysql_stmt_prepare(stmt, query, strlen(query)))
    {
        std::cerr << "mysql_stmt_prepare() failed: " << mysql_stmt_error(stmt) << std::endl;
        mysql_stmt_close(stmt);
        return false;
    }

    // 绑定参数
    MYSQL_BIND bind[1];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = (char *)name.c_str();
    bind[0].buffer_length = name.length();
    bind[0].is_null = 0;
    bind[0].length = 0;
    if (mysql_stmt_bind_param(stmt, bind))
    {
        std::cerr << "mysql_stmt_bind_param() failed: " << mysql_stmt_error(stmt) << std::endl;
        mysql_stmt_close(stmt);
        return false;
    }

    // 执行语句
    if (mysql_stmt_execute(stmt))
    {
        std::cerr << "mysql_stmt_execute() failed: " << mysql_stmt_error(stmt) << std::endl;
        mysql_stmt_close(stmt);
        return false;
    }
    // 关闭语句
    mysql_stmt_close(stmt);
    // 这里可以添加删除成功的提示
    std::cout << "Hero " << name << " deleted successfully." << std::endl;

    return true;
}


bool hero::createHero(long key, std::string name, std::string email)
{
    // 这里可以实现创建英雄的逻辑
    MYSQL_STMT *stmt = mysql_stmt_init(mConn);
    if (!stmt)
    {
        std::cerr << "mysql_stmt_init() failed" << std::endl;
        return false;
    }

    // 准备 SQL 语句
    const char *query = "INSERT INTO users (id, name, email) VALUES (?, ?, ?)";
    if (mysql_stmt_prepare(stmt, query, strlen(query)))
    {
        std::cerr << "mysql_stmt_prepare() failed: " << mysql_stmt_error(stmt) << std::endl;
        mysql_stmt_close(stmt);
        return false;
    }

    // 绑定参数
    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &key;
    bind[0].buffer_length = sizeof(key);
    bind[0].is_null = 0;
    bind[0].length = 0;

    bind[1].buffer_type = MYSQL_TYPE_STRING;
    bind[1].buffer = (char *)name.c_str();
    bind[1].buffer_length = name.length();
    bind[1].is_null = 0;
    bind[1].length = 0;

    bind[2].buffer_type = MYSQL_TYPE_STRING;
    bind[2].buffer = (char *)email.c_str();
    bind[2].buffer_length = email.length();
    bind[2].is_null = 0;
    bind[2].length = 0;

    if (mysql_stmt_bind_param(stmt, bind))
    {
        std::cerr << "mysql_stmt_bind_param() failed: " << mysql_stmt_error(stmt) << std::endl;
        mysql_stmt_close(stmt);
        return false;
    }

    // 执行语句
    if (mysql_stmt_execute(stmt))
    {
        std::cerr << "mysql_stmt_execute() failed: " << mysql_stmt_error(stmt) << std::endl;
        mysql_stmt_close(stmt);
        return false;
    }
    // 关闭语句
    mysql_stmt_close(stmt);
    // 这里可以添加创建成功的提示
    std::cout << "Hero " << name << " created successfully." << std::endl;
    return true;
}

std::string hero::getName()
{
    // 这里可以返回一个具体的名字
    return "mapan";
}

void hero::setName(std::string name)
{
    // 这里可以设置一个具体的名字
    mHeroName = name;
}

bool hero::connectToDatabase()
{
    // 这里可以实现连接数据库的逻辑
    mConn = mysql_init(nullptr);
    if (mConn == nullptr)
    {
        std::cerr << "mysql_init() failed" << std::endl;
        return false;
    }

    // 连接到数据库
    if (mysql_real_connect(mConn, "127.0.0.1", "root", "root", "testdb", 3306, nullptr, 0) == nullptr)
    {
        std::cerr << "mysql_real_connect() failed: " << mysql_error(mConn) << std::endl;
        mysql_close(mConn);
        return false;
    }    

    // 返回连接成功与否
    return true;
}

bool hero::disconnectFromDatabase()
{
    // 这里可以实现断开数据库连接的逻辑
    if (mConn != nullptr)
    {
        mysql_close(mConn);
        mConn = nullptr;
    }
    // 返回断开成功与否
    return true;
}

